//
//  BaseGeneralView.m
//  SleepApp
//
//  Created by chuangqi on 2021/4/6.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "BaseGeneralView.h"
#import "UIView+BlockGesture.h"
@interface BaseGeneralView ()<BBaseGeneralViewDataSource>{
    Base_General_Show_type _show_type;
}
#pragma mark -- 默认参数 不参与逻辑处理
/** 默认数据,不参与逻辑处理。 */
@property (nonatomic,strong) NSMutableArray<id> *dataList;
/** 蒙版透明度 */
@property (nonatomic,assign) float maskAlpha;
/** 关闭回调 */
@property(nonatomic,copy) CloseBlock CloseBlock;

@end
@implementation BaseGeneralView
- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        _show_type = Base_General_Show_type_Default;
        if ([self respondsToSelector:@selector(showViewLocation)]){
            _show_type = [self showViewLocation];
        }
        [self addSubview:self.contentView];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (_show_type == Base_General_Show_type_BottonSafe){
                make.edges.insets(UIEdgeInsetsMake(0, 0, kBottomSafeHeight, 0));
            } else {
                make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
            }
        }];
    }
    return self;
}
#pragma mark -- 公开链式语法
/** 初始化 */
+ (BaseGeneralView *(^)(void)) init_wl{
    return ^id (){
        return [[self.class alloc] init];
    };
}
/** 蒙板透明度 */
- (BaseGeneralView *(^)(float alpha)) maskAlpha_wl{
    return ^id (float alpha){
        self.maskAlpha = alpha;
        return self;
    };
}
/** 添加父级控制器 */
- (BaseGeneralView *(^)(UIViewController *rootViewController)) rootViewController_wl{
    return ^id (UIViewController *rootViewController){
        self.rootViewController = rootViewController;
        return self;
    };
}
/** 初始化数据 */
- (BaseGeneralView *(^)(id data)) addData_wl{
    return ^id (id data){
        [self.dataList addObject:data];
        return self;
    };
}
/** 添加关闭 */
- (BaseGeneralView *(^)(CloseBlock CloseBlock)) closeBlock_wl{
    return ^id (CloseBlock CloseBlock){
        self.CloseBlock = CloseBlock;
        return self;
    };
}
/** 弹出内容 */
- (BaseGeneralView *(^)(void)) show_wl{
    return ^id (){
        [self defaultShowView];
        return self;
    };
}

#pragma mark -- 内部默认逻辑

- (void)defaultShowView{
    //逻辑判断是否需要显示弹窗
    if ([self respondsToSelector:@selector(isShowModelList:isShowBlock:)]){
        __WEAK_SELF_YLSLIDE
        [self isShowModelList:self.dataList isShowBlock:^{
            [weakSelf _showView];
        }];
    } else {
        [self _showView];
    }
}
/** 展示视图 */
- (void)_showView{
//    __WEAK_SELF_YLSLIDE
    //初始化展示内容判断
    if ([self respondsToSelector:@selector(initView)]){
        [self initView];
    } else {
        NSLog(@"没有添加初始化页面代理");
    }
    
    /** 展示的地方 */
    UIView *keyView = nil;
    if (self.rootViewController){
        keyView = self.rootViewController.view;
    } else {
        keyView = [UIApplication sharedApplication].keyWindow;
    }
    
    //添加背景蒙层
    UIVisualEffectView *bgView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    bgView.alpha = 0;
    bgView.userInteractionEnabled = YES;
    
    objc_setAssociatedObject(self,"bgView",bgView,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [bgView addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [self defaultCloseView];
    }];
    
    [keyView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    //走自定义的展示方式
    if ([self respondsToSelector:@selector(showMaskView:)]){
        [self showMaskView:bgView];
    }
    //走默认的展示方式
    else {
        [UIView animateWithDuration:0.4f animations:^{
            bgView.alpha = self.maskAlpha;
        }];
    }
    
    
    
    [keyView addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        //主视图自定义的样式
        if ([self respondsToSelector:@selector(initViewMakeConstraints:fromView:)]){
            [self initViewMakeConstraints:make fromView:keyView];
        }
        //主视图默认的样式
        else {
            make.left.right.bottom.mas_equalTo(keyView);
            make.height.mas_equalTo(keyView.mas_height).multipliedBy(.5f);
        }
    }];
    //主视图自定义展示样式
    if ([self respondsToSelector:@selector(showView:fromView:)]){
        [self showView:self fromView:keyView];
    }
    //主视图默认展示样式
    else {
        switch (_show_type) {
            case Base_General_Show_type_Top:
                self.transform = CGAffineTransformMakeTranslation(0,-keyView.frame.size.height);
                break;
            case Base_General_Show_type_Lift:
                self.transform = CGAffineTransformMakeTranslation(-keyView.frame.size.width,0);
                break;
            case Base_General_Show_type_Right:
                self.transform = CGAffineTransformMakeTranslation(keyView.frame.size.width,0);
                break;
            case Base_General_Show_type_Botton:
            default:
                self.transform = CGAffineTransformMakeTranslation(0,keyView.frame.size.height);
                break;
        }
        [UIView animateWithDuration:0.4f animations:^{
            self.transform = CGAffineTransformIdentity;
            self.alpha = 1.f;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2f animations:^{
                switch (self->_show_type) {
                    case Base_General_Show_type_Top:
                        self.transform = CGAffineTransformMakeTranslation(0,-5);
                        break;
                    case Base_General_Show_type_Lift:
                        self.transform = CGAffineTransformMakeTranslation(-5,0);
                        break;
                    case Base_General_Show_type_Right:
                        self.transform = CGAffineTransformMakeTranslation(5,0);
                        break;
                    case Base_General_Show_type_Botton:
                    default:
                        self.transform = CGAffineTransformMakeTranslation(0,5);
                        break;
                }
            }];
        }];
    }
    //主动布局
    [self layoutIfNeeded];
    
    //初始化展示内容数据
    if ([self respondsToSelector:@selector(initData:)]){
        [self initData:self.dataList];
    } 
}
/** 默认关闭视图 内部 */
- (void)defaultCloseView{
    UIView *bgView = objc_getAssociatedObject(self, "bgView");
    //自定义的关闭视图样式
    if ([self respondsToSelector:@selector(closeView:maskView:)]){
        [self closeView:self maskView:bgView];
    }
    //默认的关闭视图样式
    else {
        [UIView animateWithDuration:0.4f animations:^{
            self.alpha = 0;
            bgView.alpha = 0;
            self.transform = CGAffineTransformMakeTranslation(0,Main_Screen_Height);
        } completion:^(BOOL finished) {
            objc_removeAssociatedObjects(self);
            [bgView removeFromSuperview];
            [self removeFromSuperview];
            [[NSNotificationCenter defaultCenter] removeObserver:self];
        }];
    }
}
/** 关闭视图的中转 */
- (void)defaultCloseView:(id)data {
    if (self.CloseBlock){
        self.CloseBlock(data);
    }
    [self defaultCloseView];
}
/** 点击确定逻辑操作 */
- (void)defaultSureCloseViewData:(id)data{
    if ([self respondsToSelector:@selector(clickSelectCloseViewData:sureBlock:)]){
        [self clickSelectCloseViewData:data sureBlock:^void(id data) {
            [self defaultCloseView:data];
        }];
    } else {
        [self defaultCloseView:nil];
    }
}

/** 初始化视图数据 */
- (void)initViewData:(NSArray *)data{
    NSLog(@"子视图必须重写该方法--%s",__func__);
}
/** 初始化视图数据 */
-(void)initData:(NSArray<id> *)dataList{
    NSLog(@"子视图必须重写该方法--%s",__func__);
}
#pragma mark -- 懒加载
- (NSMutableArray<id> *)dataList{
    if (!_dataList){
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}
-(UIView *)contentView{
    if (!_contentView){
        _contentView = [[UIView alloc]init];
    }
    return _contentView;
}
@end
